package com.burt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import com.burt.domain.Person;
import com.burt.repository.PersonRepository;

/**
 * @author luowei1
 * @date 2018/7/4 15:59
 */
@RestController
public class PersonRestController {

    private final PersonRepository personRepository;

    @Autowired
    public PersonRestController(PersonRepository personRepository) {
        this.personRepository = personRepository;
    }

    @GetMapping("/person/{id}")
    public Person person(@PathVariable Long id, @RequestParam(required = false) String name) {
        Person person = new Person();
        person.setId(id);
        person.setName(name);

        return person;
    }

    @PostMapping(value = "/person/json/to/properties", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = "application/properties+person")
    public Person personJsonToProperties(@RequestBody Person person) {
        return person;
    }

    @PostMapping(value = "/person/properties/to/json", consumes = "application/properties+person", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Person personPropertiesToJson(@RequestBody Person person) {
        return person;
    }

    @PostMapping("/web/mvc/person/save")
    public Boolean save(@RequestBody Person person) {
        return personRepository.save(person);
    }

    @PostMapping("/web/mvc/exception")
    public Boolean exception() {
        throw new RuntimeException("故意抛异常");
    }

}
